using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    // 定义游戏状态的枚举类
    public enum GameState
    {
        Playing,
        Pause,
        GameOver
    }

    public GameState gameState = GameState.Playing;

    // 单例模式
    private static GameManager _instance;

    public static GameManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindAnyObjectByType<GameManager>();
            }

            return _instance;
        }
    }

    // 炸弹数量
    private int bombCount = 0;

    // 游戏分数
    public int score = 0;

    // 检测双击事件
    private float _lastClickTime;
    public float clickInterval = 0.2f;

    // 使用炸弹音效播放器
    public AudioSource boomSound;

    // GameOver音效
    public AudioSource gameOverSound;


    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        DoubleClick();
    }

    // 炸弹+1
    public void AddBomb()
    {
        bombCount++;
        UIManager.Instance.BoomCountUpdate(bombCount);
    }

    //分数
    public void AddScore(int score)
    {
        this.score += score;
        UIManager.Instance.TextScoreUpdate(this.score);
    }

    public void ShowPlayerHp(int hp)
    {
        UIManager.Instance.TextPlayerHpUpdate(hp);
    }

    //暂停游戏
    public void PauseGame()
    {
        Time.timeScale = 0;
        gameState = GameState.Pause;
    }

    //继续游戏
    public void ResumeGame()
    {
        Time.timeScale = 1;
        gameState = GameState.Playing;
    }

    public void RestartGame()
    {
        ResumeGame();
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void QuitGame()
    {
        Application.Quit();
    }

    public void DoubleClick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (_lastClickTime == 0)
            {
                _lastClickTime = Time.time;
                return;
            }
            else
            {
                if (Time.time - _lastClickTime < clickInterval)
                {
                    // 双击事件
                    // 获取player
                    Player player = FindFirstObjectByType<Player>();
                    if (!player.isDead)
                    {
                        UseBoom();
                    }
                }

                _lastClickTime = Time.time;
            }
        }
    }

    void UseBoom()
    {
        if (bombCount <= 0)
        {
            return;
        }

        boomSound.Play();
        bombCount--;
        UIManager.Instance.BoomCountUpdate(bombCount);
        //获取所有enemy
        //1、根据脚本名称获取所有的enemy component
        // Enemy[] enemies = FindObjectsByType("Enemy");
        // foreach (Enemy enemy in enemies)
        // {
        //     enemy.TakeDamage();
        // }
        // 2、通过标签 过的所有enemy GameObject
        GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
        foreach (GameObject enemy in enemies)
        {
            enemy.GetComponent<Enemy>().TakeDamage(99999);
        }
    }

    public void GameOver()
    {
        gameOverSound.Play();
        PauseGame();
        // 销毁所有敌人
        GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
        foreach (GameObject enemy in enemies)
        {
            Destroy(enemy);
        }

        UIManager.Instance.ShowGameOverUI();
    }
}